package com.supermap.dataservice.cloud.productC;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @version v1.0.0
 * @Author:zhoujian
 * @Desciption:
 * @date 2019-04-18 10:46
 */
@Mapper
public interface GridProductFromChnMapper {

    /**
     * 查询产品json列表
     * @param dataContent 数据内容
     * @param elementCode 要素code
     * @param areaCode 区域码
     * @return
     */
    @Select("SELECT t.id as productId,t.product_date as ctime, t.json_url as jsonurl , t.png_url as pngurl\n" +
            "FROM t_grid_product t\n" +
            "WHERE t.data_content=#{dataContent} AND t.element_code = #{elementCode} AND t.area_code=#{araeCode}\n" +
            "GROUP BY product_date,element_code\n" +
            "ORDER BY product_date DESC")
    public List<Map<String,Object>> selectProductByContentElementArea(@Param("dataContent")String dataContent , @Param("elementCode") String elementCode ,@Param("araeCode") String areaCode);

    /**
     * 获取一级目录
     * @param type
     * @param areaCode
     * @return
     */
    @Select("select m.code,m.name, \n" +
            "\t\t\t\tcase from_system\n" +
            "            when 'DAP' then a.areaname\n" +
            "            else '全国'\n" +
            "            end areaname,\n" +
            "            case from_system\n" +
            "            when 'DAP' then 'local'\n" +
            "            else 'china'\n" +
            "            end `type`\n" +
            "            from master_codeinfo m \n" +
            "               inner join t_grid_product t on m.code = t.data_content\n" +
            "               inner join t_area a on a.areacode = t.area_code \n" +
            "               where  t.area_code = if(left(#{areaCode},2) = '50',#{areaCode},left(#{areaCode},2)) \n" +
            "               and \n" +
            "               case #{type}\n" +
            "               when 'china' then from_system <> 'DAP'\n" +
            "               when 'local' then from_system = 'DAP'  \n" +
            "               else 1=1\n" +
            "               end \n" +
            "               group by m.code order by `type`;")
    public List<Map<String,Object>> firstCatalog(@Param("type") String type , @Param("areaCode") String areaCode);

    /*@Select("select code,name,\"local\" as `type` from master_codeinfo  where  code in \n" +
            "(\n" +
            "select distinct(data_content) from t_grid_product where from_system = #{systemName}\n" +
            ")")
    public List<Map<String,Object>> localFirstCatalog(@Param("systemName") String systemName);*/

    @Select("select element_code,replace(group_concat(name order by pos ),',','') name,tt.png_url as pngurl , tt.iconurl from \n" +
            " (select t.id, t.element_code,m.name,m.code,LOCATE(m.code,t.element_code_r) pos , t.png_url ,m.iconurl from  master_codeinfo m \n" +
            "  inner join \n" +
            "   (select t.id, replace(element_code,'-',',') element_code_r,element_code , t.png_url  from t_grid_product t where t.data_content = #{dataContent} and t.area_code = #{areaCode}\n" +
            "\t\t\t\t group by element_code) t\n" +
            "   on find_in_set(m.code , t.element_code_r) order by t.id,pos) tt \n" +
            "   group by id")
    public List<Map<String,Object>> selectElementByDataContent(@Param("dataContent") String dataContent , @Param("areaCode") String areaCode);


}